﻿using NPOI.SS.UserModel;
using System;

namespace NewfarTormentTool.core
{
    public static class Tools
    {
        /// <summary>
        /// 获取Excel单元格的字符串
        /// </summary>
        public static string get_cell_str(ICell tCell)
        {
            if (tCell == null)
            {
                return "";
            }
            string tempValue = "";
            switch (tCell.CellType)
            {
                case CellType.Blank:
                    break;
                case CellType.Boolean:
                    tempValue = tCell.BooleanCellValue.ToString();
                    break;
                case CellType.Error:
                    break;
                case CellType.Formula:
                    IFormulaEvaluator fe = WorkbookFactory.CreateFormulaEvaluator(tCell.Sheet.Workbook);
                    var cellValue = fe.Evaluate(tCell);
                    switch (cellValue.CellType)
                    {
                        case CellType.Blank:
                            break;
                        case CellType.Boolean:
                            tempValue = cellValue.BooleanValue.ToString();
                            break;
                        case CellType.Error:
                            break;
                        case CellType.Formula:
                            break;
                        case CellType.Numeric:
                            tempValue = cellValue.NumberValue.ToString();
                            break;
                        case CellType.String:
                            tempValue = cellValue.StringValue.ToString();
                            break;
                        case CellType.Unknown:
                            break;
                        default:
                            break;
                    }
                    break;
                case CellType.Numeric:
                    if (DateUtil.IsCellDateFormatted(tCell))
                    {
                        DateTime? dt = tCell.DateCellValue;
                        if (dt != null)
                        {
                            tempValue = dt.Value.ToString("yyyy-MM-dd");
                        }
                        else
                        {
                            tempValue = "";
                        }
                    }
                    else
                    {
                        tempValue = tCell.NumericCellValue.ToString();
                    }
                    break;
                case CellType.String:
                    tempValue = tCell.StringCellValue.Trim();
                    break;
                case CellType.Unknown:
                    break;
                default:
                    break;
            }
            return tempValue;
        }
    }
}
